package org.rockyang.dike.surpport;


import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangjian
 */
public class ShiroxConfigure {

	/**
	 * 登录 URL
	 */
	private String loginUrl;

	/**
	 * 未授权时跳转的 URL
	 */
	private String unAuthorizedUrl;

	/**
	 * URL 白名单，这些地址全部放行
	 */
	private List<String> whiteList = new ArrayList<>();

	/**
	 * URL 黑名单，这些路径直接全部拦截
	 */
	private List<String> blackList = new ArrayList<>();

	public ShiroxConfigure() {}

	public String getUnAuthorizedUrl() {
		return unAuthorizedUrl;
	}

	public void setUnAuthorizedUrl(String unAuthorizedUrl) {
		this.unAuthorizedUrl = unAuthorizedUrl;
	}

	/**
	 * 添加白名单
	 * @param url
	 */
	public void addBlackList(String url) {
		blackList.add(url);
	}

	/**
	 * 判断某个URI请求是否在白名单
	 * @param url
	 * @return
	 */
	public boolean isInBlackList(String url) {

		if (0 == blackList.size()) {
			return false;
		}
		return blackList.contains(url);
	}

	/**
	 * 添加白名单
	 * @param url
	 */
	public void addWhiteList(String url) {
		if (null != url) {
			whiteList.add(url);
		}
	}

	/**
	 * 判断某个 URL 是否在白名单内
	 * @param url
	 */
	public boolean isInWhiteList(String url)
	{
		if (0 == whiteList.size()) {
			return false;
		}
		if (StringUtils.equals(url,getLoginUrl())) {
			return true;
		}
		return whiteList.contains(url);
	}

	public String getLoginUrl() {
		return loginUrl;
	}

	public void setLoginUrl(String loginUrl) {
		this.loginUrl = loginUrl;
	}
}
